from django.http import JsonResponse
from django.views.generic import View
import inspect
from ..models import GlobalTables

class GlobalTableTypesView(View):
     def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            global_tables = [member for name, member in inspect.getmembers(GlobalTables) 
                             if inspect.isclass(member) and member.__module__ == 'literature.models.GlobalTables']
            # print(global_tables)
            # for gb in global_tables:
            #     print(gb.__module__)
            
            classes_info = []
            for model_class in global_tables:
                classes_info.append({
                    'uuid':  model_class.__name__,
                    'display_str': model_class._meta.verbose_name + '(' + model_class.__name__ + ')',
                    'name': model_class.__name__,
                    'zh_name': model_class._meta.verbose_name,
                })
            
            res_data = {}
            res_data["total_count"] = len(classes_info)
            res_data["options"] = classes_info
            res["code"] = 200
            res["message"] = "查询成功"
            res["data"] = res_data
            # print(res_data)
            return JsonResponse(res)
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)